package cn.limeizi.ssm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.limeizi.ssm.po.Work;
import cn.limeizi.ssm.service.WorkService;

@Controller
@RequestMapping("/admin")
public class AdminWorkController {
	
	@Autowired
	private WorkService workService;
	
	@RequestMapping(value="/adminWork.action",method=RequestMethod.GET)
	public String index(Model model){
		
		List<Work> workList = workService.selectAll();
		
		model.addAttribute("workList", workList);
		
		return "admin/work/workList";		
	}
	
	@RequestMapping(value="/toEditWork.action",method=RequestMethod.GET)
	public String toEdit(Model model,Integer id){
		
		if(id!=null){
			
			Work work=workService.selectWorkById(id);
			
			model.addAttribute("work", work);
		}
		
		return "admin/work/edit";
		
	}
	
	@RequestMapping(value="/editWork.action",method=RequestMethod.POST)
	public String edit(Work work){
		
		if(work.getId()!=null){
			workService.updateWork(work);
		}else{
			workService.insertWork(work);
		}
		
		return "redirect:adminWork.action";		
	}
	
	@RequestMapping(value="deleteWork.action",method=RequestMethod.GET)
	public String delete(Integer id){
		
		workService.deleteWork(id);
		return "redirect:adminWork.action";
		
	}

}
